<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class SpecialController extends Controller
{
    //
    public function edit($id)
    {
    	$allData = DB::table('games') -> select('id','pid','name','path',DB::raw("concat(path,',',id) AS sort_str")) -> orderBy('sort_str') -> get();
    	foreach($allData as $key => $value)
    	{
    		$num = substr_count($value -> path,',');
    		$str = str_repeat('|----', $num);
    		$allData[$key] -> name = $str.$value -> name;
    	}
    	
    	$data = DB::table('games') -> where('id',$id) -> first();
        $data -> time = date('Ymd',$data -> time);

    	return view('admin.games.edit',['data' => $data,'allData' => $allData]);
    }

    public function update(Request $request,$id)
    {
    	$data = $request -> except('_token');
    	if($data['pid'] == '0')
    	{
    		$path = '0';
    	}else
    	{
    		$pdata = DB::table('games') -> where('id',$data['pid']) -> first();
    		$path = $pdata -> path.','.$data['pid'];
    	}

    	$data['path'] = $path;

        //获取原来游戏文件的信息
        $oldData = DB::table('games') -> where('id',$id) -> first();

        //处理图片
        if($request -> hasFile('logo'))
        {
            //获取扩展名
            if($request -> file('logo') -> isValid())
            {
                $time = time();
                $extension = $request -> file('logo') -> getClientOriginalExtension();
                $fileName = str_random(32).'.'.$extension;
                $d = date('Ymd');
                $dir = './uploads/games/imgs/'.$d;

                if(!file_exists($dir))
                {
                    mkdir($dir,0777,true);

                }

                //移动文件
                $move = $request -> file('logo') -> move($dir,$fileName);

                if($move)
                {
                    //获取原来logo路径
                    $oldDir = './uploads/games/imgs/'.date('Ymd',$oldData -> time).'/'.$oldData -> logo;
                    if(file_exists($oldDir))
                    {
                        unlink($oldDir);
                    }
                    $data['logo'] = $fileName;
                   
                }
            }
        }

        //处理文件
        if($request -> hasFile('file'))
        {
            //获取扩展名
            if($request -> file('file') -> isValid())
            {
                $time = time();
                $extension = $request -> file('file') -> getClientOriginalExtension();
                $fileName = str_random(32).'.'.$extension;
                $d = date('Ymd');
                $dir = './uploads/games/files/'.$d;

                if(!file_exists($dir))
                {
                    mkdir($dir,0777,true);

                }
                //移动文件
                $move = $request -> file('file') -> move($dir,$fileName);

                if($move)
                {
                    //获取原来文件路径
                    $oldDir = './uploads/games/imgs/'.date('Ymd',$oldData -> time).'/'.$oldData -> file;
                    if(file_exists($oldDir))
                    {
                        unlink($oldDir);
                    }

                    $data['file'] = $fileName;
                  
                }
            }
        }

        $data['time'] = $time;
    	$res = DB::table('games') -> where('id',$id) -> update($data);

    	if($res)
    	{
    		return redirect('/admin/games/index') -> with(['info' => '编辑成功']);
    	}else
    	{
    		return back() -> with(['info' => '编辑失败']);
    	}
    }
}
